﻿using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace AgLux
{
    /// <summary>
    /// Represents a component that displays a hyper link.
    /// </summary>

    public class Link : Component
    {
        protected TextBlock _text;
        protected string _url;
        protected bool _isPressed;

        /// <summary>
        /// Initializes a new instance of the Link class.
        /// </summary>

        /// <param name="url">
        /// The link URL.
        /// </param>

        /// <param name="text">
        /// The link text.
        /// </param>

        public Link(string url, string text)
            : base()
        {
            _text = (TextBlock)_resource.FindName("text");
            _url = url;
            _isPressed = false;

            _text.Text = text;
            if (text == null)
            {
                _text.Text = url;
            }
            _text.SetValue(TextBlock.ForegroundProperty, "#FF0000FF");
            _text.TextDecorations = TextDecorations.Underline;

            _text.MouseEnter += new MouseEventHandler(BeEntered);
            _text.MouseLeave += new EventHandler(BeLeft);
            _text.MouseLeftButtonDown += new MouseEventHandler(BePressed);
        }

        /// <summary>
        /// Prepare to display the component.
        /// </summary>

        /// <param name="width">
        /// The width of the canvas on which the component will be displayed.
        /// </param>

        public override void Prepare(double width)
        {
            Width = _screen.Width = _text.Width = width;
            Height = _screen.Height = _text.Height = _text.ActualHeight;
        }

        /// <summary>
        /// Occurs when the component is entered.
        /// </summary>

        /// <param name="sender">
        /// The object that raised the event.
        /// </param>

        /// <param name="args">
        /// The MouseEventArgs that contains the event data.
        /// </param>

        protected void BeEntered(object sender, MouseEventArgs args)
        {
            _text.SetValue(TextBlock.ForegroundProperty, "#FFFF0000");
        }

        /// <summary>
        /// Occurs when the component is left.
        /// </summary>

        /// <param name="sender">
        /// The object that raised the event.
        /// </param>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        protected void BeLeft(object sender, EventArgs args)
        {
            if (_isPressed)
            {
                _text.SetValue(TextBlock.ForegroundProperty, "#FF9900FF");
            }

            else
            {
                _text.SetValue(TextBlock.ForegroundProperty, "#FF0000FF");
            }
        }

        /// <summary>
        /// Occurs when the component is pressed.
        /// </summary>

        /// <param name="sender">
        /// The object that raised the event.
        /// </param>

        /// <param name="args">
        /// The MouseEventArgs that contains the event data.
        /// </param>

        protected void BePressed(object sender, MouseEventArgs args)
        {
            _isPressed = true;
            _text.SetValue(TextBlock.ForegroundProperty, "#FF9900FF");
            HtmlPage.Navigate(_url, "");
        }
    }
}
